/**
 *
 * latex_reques(obj,key)
 * @param {Object} 试题列表
 * @param {String} json对象 key 名
 * */
export function latex_reques(obj, key) {
    obj.forEach(item => {
        if (!item[key]) return false;
        /**
         * 规则
         * */
        var matchReg1 = /\$\$.*?\$\$/g; //规则1 公式以$$开头和结尾
        var matchReg2 = /\$.*?\$/g; //规则2 公式以$ 开头和结尾
        // var matchReg3 = /\$\S.*?\$/g; //规则3  此规则比较特殊，存在此规则是当存在两个公式连贯出现时 会组合成 以$$ 开头 以$ 结尾的特殊组合
        var latex_match_reg = /<img class="latex" src="https:\/\/latex.codecogs.com\/svg.latex\?">.*?<img class="latex" src="https:\/\/latex.codecogs.com\/svg.latex\?">/g;

        /**
         * 试题标题 latex 公式解析
         * */
        var replaceTitle = item[key]
            .replace(/&lt;/g, "<")
            .replace(/&gt;/g, ">")
            .replace(/&amp;lt;/g, "<")
            .replace(/&amp;gt;/g, ">"); //首选将公式中的 < > 于号转义回来
        var title = replaceTitle.match(matchReg2);
        //第一次过滤 $$   $$
        if (title != null) {
            title.forEach(v => {
                var imgUrl = encodeURI(
                    "https://latex.codecogs.com/svg.latex?" +
                        v.replace(/\$/g, "")
                );
                replaceTitle = replaceTitle
                    .replace(/&lt;/g, "<")
                    .replace(/&gt;/g, ">")
                    .replace(v, '<img class="latex" src="' + imgUrl + '">');
            });
            item[key] = replaceTitle;
        }

        //北京试题库图片和现系统试题库 图片转换
        if (item.qid == null) {
            item[key] = replaceTitle
                .replace(/&lt;/g, "<")
                .replace(/&gt;/g, ">")
                .replace(/&quot;/g, '"')
                .replace(/&amp;nbsp;/g, "  ")
                .replace(/&amp;#39;/g, "'")
                .replace(
                    /src="https:\/\/lexiao.limeiti.cn\/Public\/qvnet/g,
                    'class="zh_img" src="https://ftp.zhizhuedu.cn/Public/qvnet'
                )
                .replace(
                    /src="\/ueditor/g,
                    'class="zh_img" src="https://ftp.zhizhuedu.cn/Public/qvnet/ueditor'
                )
                .replace(/&amp;gt;/g, ">");
        } else {
            item[key] = replaceTitle
                .replace(/&lt;/g, "<")
                .replace(/&gt;/g, ">")
                .replace(/&quot;/g, '"')
                .replace(/&amp;nbsp;/g, "  ")
                .replace(/&amp;#39;/g, "'")
                .replace(
                    /src=\\https:\/\/gallery.fbcontent.cn\/latex?/g,
                    'class="latex" src="https://gallery.fbcontent.cn/latex'
                )
                .replace(
                    /src="https:\/\/caizi.st.netos.cn\/ueditor/g,
                    'class="bj_img_w" src="https://wechat.zhizhuedu.cn/ueditor'
                )
                .replace(
                    /src="https:\/\/lexiao.limeiti.cn\/ueditor/g,
                    'class="bj_img_w" src="https://wechat.zhizhuedu.cn/ueditor'
                )
                .replace(
                    /src="\/ueditor/g,
                    'class="bj_img_w" src="https://wechat.zhizhuedu.cn/ueditor'
                )
                .replace(
                    /src="data:image/g,
                    'class="base_img_title" src="data:image'
                )
                .replace(/&amp;gt;/g, ">");
        }

        //第二次过滤
        var $_str = item[key].match(matchReg1);
        var replaceTitle_str = item[key];
        if ($_str != null) {
            $_str.forEach(v => {
                var imgUrl = encodeURI(
                    "https://latex.codecogs.com/svg.latex?" +
                        v.replace(/\$/g, "")
                );
                replaceTitle_str = replaceTitle_str
                    .replace(/&lt;/g, "<")
                    .replace(/&gt;/g, ">")
                    .replace(v, '<img class="latex" src="' + imgUrl + '">');
            });
            item[key] = replaceTitle_str;
        }

        //第三次过滤
        var latex_img = item[key].match(latex_match_reg);
        var latex_title = item[key];
        if (latex_img != null) {
            latex_img.forEach(v => {
                var imgUrl = encodeURI(
                    "https://latex.codecogs.com/svg.latex?" +
                        v.replace(
                            /<img class="latex" src="https:\/\/latex.codecogs.com\/svg.latex\?">/g,
                            ""
                        )
                );
                latex_title = latex_title
                    .replace(/&lt;/g, "<")
                    .replace(/&gt;/g, ">")
                    .replace(v, '<img class="latex" src="' + imgUrl + '">');
            });
            item[key] = latex_title;
        }

        return item[key].replace(/<(?!img).*?>/g, "");
    });
}

/**
 * 答案选项
 * latex_answer(obj,key)
 * @param {Object} 试题列表
 * @param {String} json对象 key 名
 * */
export function latex_answer(obj, key) {
    obj.forEach(item => {
        if (!item[key]) return false;
        /**
         * 规则
         * */
        var matchReg1 = /\$\$.*?\$\$/g; //规则1 公式以$$开头和结尾
        var matchReg2 = /\$.*?\$/g; //规则2 公式以$ 开头和结尾
        // var matchReg3 = /\$\S.*?\$/g; //规则3  此规则比较特殊，存在此规则是当存在两个公式连贯出现时 会组合成 以$$ 开头 以$ 结尾的特殊组合
        var latex_match_reg = /<img class="latex" src="https:\/\/latex.codecogs.com\/svg.latex\?">.*?<img class="latex" src="https:\/\/latex.codecogs.com\/svg.latex\?">/g;
        /**
         * 答案选项 latex 公式解析
         * */
        var answerAry = item[key];
        answerAry.forEach(v => {
            var replaceAnswerList = v.answer
                .replace(/&lt;/g, "<")
                .replace(/&gt;/g, ">")
                .replace(/&quot;/g, '"')
                .replace(/&amp;lt;/g, "<")
                .replace(/&amp;gt;/g, ">");
            var analysis = replaceAnswerList.match(matchReg1);

            if (analysis != null) {
                analysis.forEach(vl => {
                    var imgUrl = encodeURI(
                        "https://latex.codecogs.com/svg.latex?" +
                            vl.replace(/\$/g, "")
                    );
                    replaceAnswerList = replaceAnswerList.replace(
                        vl,
                        '<img class="latex" src="' + imgUrl + '">'
                    );
                });
                v.answer = replaceAnswerList;
            }
            //北京试题库图片和现系统试题库 图片转换
            if (item.qid == null) {
                v.answer = replaceAnswerList
                    .replace(/&lt;/g, "<")
                    .replace(/&gt;/g, ">")
                    .replace(/&amp;nbsp;/g, "  ")
                    .replace(/&amp;#39;/g, "'")
                    .replace(
                        /src="\/ueditor/g,
                        'src="https://ftp.zhizhuedu.cn/Public/qvnet/ueditor'
                    )
                    .replace(/&amp;gt;/g, ">");
            } else {
                v.answer = replaceAnswerList
                    .replace(/&lt;/g, "<")
                    .replace(/&gt;/g, ">")
                    .replace(/&amp;nbsp;/g, "  ")
                    .replace(/&amp;#39;/g, "'")
                    .replace(
                        /src="https:\/\/caizi.st.netos.cn\/ueditor/g,
                        'class="bj_img_w" src="https://wechat.zhizhuedu.cn/ueditor'
                    )
                    .replace(
                        /src=\\https:\/\/gallery.fbcontent.cn\/latex?/g,
                        'class="latex" src="https://gallery.fbcontent.cn/latex'
                    )
                    .replace(
                        /src=\\\/ueditor/g,
                        'class="bj_img" src="https://wechat.zhizhuedu.cn/ueditor'
                    )
                    .replace(new RegExp("\\.png", "g"), '.png"')
                    .replace(
                        /src=\\https:\/\/caizi.st.netos.cn/g,
                        'class="latex" src="https://wechat.zhizhuedu.cn'
                    )
                    .replace(
                        /src=\\data:image/g,
                        'class="base_img" src="data:image'
                    )
                    .replace(/\\\//g, '"')
                    .replace(/&amp;gt;/g, ">");
            }
            //第二次过滤
            var $_str = v.answer.match(matchReg2);
            var replaceTitle_str = v.answer;
            if ($_str != null) {
                $_str.forEach(v => {
                    var imgUrl = encodeURI(
                        "https://latex.codecogs.com/svg.latex?" +
                            v.replace(/\$/g, "")
                    );
                    replaceTitle_str = replaceTitle_str
                        .replace(/&lt;/g, "<")
                        .replace(/&gt;/g, ">")
                        .replace(v, '<img class="latex" src="' + imgUrl + '">');
                });
                v.answer = replaceTitle_str.replace(/<(?!img).*?>/g, "");
            }

            //第三次过滤
            var latex_img = v.answer.match(latex_match_reg);
            var latex_title = v.answer;

            if (latex_img != null) {
                latex_img.forEach(v => {
                    var imgUrl = encodeURI(
                        "https://latex.codecogs.com/svg.latex?" +
                            v.replace(
                                /<img class="latex" src="https:\/\/latex.codecogs.com\/svg.latex\?">/g,
                                ""
                            )
                    );
                    latex_title = latex_title
                        .replace(/&lt;/g, "<")
                        .replace(/&gt;/g, ">")
                        .replace(v, '<img class="latex" src="' + imgUrl + '">');
                });
                v.answer = latex_title.replace(/<(?!img).*?>/g, "");
            }
            v.answer = v.answer.replace(/<(?!img).*?>/g, "");
        });

        return answerAry;
    });
}
export function Replace_str(content, class_style, base_style) {
    var text = content.replace(/<\/?((?!img).)*?\/?>/g, "");
    text = text.replace(/src="http/g, class_style + ' src="http');
    text = text.replace(/src="data:/g, base_style + ' src="data:');
    text = text.replace(/src=\\"/g, class_style + ' src="');
    text = text.replace(
        /src="\/ueditor/g,
        class_style + ' src="http://lexiao.app.limeiti.cn/ueditor'
    );
    text = text.replace(/.png\\/g, ".png");
    text = text.replace(/.jpg\\/g, ".jpg");
    text = text.replace(/\[|]/g, "");
    text = text.replace(/&nbsp;/gi, "");
    return text;
}

export function getAnalysis(data, interest_lists_log = "interest_lists_log") {
    var letter = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"];
    data.forEach(item => {
        item.question_type_name = getTypeName(item.question_typeid);
        item.answer = "";
        item.myanswer = "";
        if (
            (item.is_one_choice == 2 && item.question_typeid != 3) ||
            item.is_many_choice == 2
        ) {
            item.answer_list.forEach((aitem, aindex) => {
                var str = "A";
                var code = str.charCodeAt();
                var str2 = String.fromCharCode(code + aindex);
                aitem.text = str2;
                aitem.lettertext = letter[aindex];
                if (aitem.is_answer == 1) {
                    item.answer += letter[aindex];
                }
                if (item[interest_lists_log][0]) {
                    item.is_answer = item[interest_lists_log][0].is_answer;
                    if (
                        typeof item[interest_lists_log][0][
                            "question_answer_id"
                        ] == "object"
                    ) {
                        item[interest_lists_log][0][
                            "question_answer_id"
                        ].forEach(laitem => {
                            if (aitem.question_answer_id == laitem) {
                                item.myanswer += letter[aindex];
                            }
                        });
                    }
                }
            });
        } else {
            if (item.answer_list) {
                item.answer_list.forEach(aitem => {
                    if (aitem.is_answer == 1) {
                        if (item.answer) {
                            item.answer += "......" + aitem.answer;
                        } else {
                            item.answer += aitem.answer;
                        }
                    }
                });
            }
            if (item[interest_lists_log].length > 0) {
                if (item[interest_lists_log][0].answer_img_list) {
                    item.answer_img_list = item[
                        interest_lists_log
                    ][0].answer_img_list.split(",");
                }
                item.voice_url = item[interest_lists_log][0].voice_url;
                item.postil = item[interest_lists_log][0].postil;
                item.is_answer = item[interest_lists_log][0].is_answer;
                if (item[interest_lists_log][0].user_text_answer.length > 0) {
                    item[interest_lists_log][0].user_text_answer.forEach(
                        val => {
                            if (item.myanswer) {
                                //答案数组已经有缓存的数据
                                if (val) {
                                    //这个空答题了
                                    item.myanswer += "......" + val;
                                } else {
                                    item.myanswer += "......" + "没答题";
                                }
                            } else {
                                //答案数组已经还没有缓存的数据
                                if (val) {
                                    item.myanswer += val;
                                } else {
                                    item.myanswer += "没答题";
                                }
                            }
                        }
                    );
                }
            }
            // if (item.question_typeid == 11) {
            //     item.question_photo = item.question_photo;
            // }
        }
    });
    return data;
}
// 题型显示
export function getTypeName(type) {
    var typeNmae = "";
    switch (type) {
        case 1:
            typeNmae = "单选题";
            break;
        case 2:
            typeNmae = "多选题";
            break;
        case 3:
            typeNmae = "判断题";
            break;
        case 4:
            typeNmae = "填空题";
            break;
        case 5:
            typeNmae = "问答题";
            break;
        case 6:
            typeNmae = "定值填空题";
            break;
        case 7:
            typeNmae = "阅读材料题";
            break;
        case 8:
            typeNmae = "材料问答填空";
            break;
        case 9:
            typeNmae = "病句修改";
            break;
        case 10:
            typeNmae = "简答题";
            break;
        case 11:
            typeNmae = "画图题";
            break;
        case 12:
            typeNmae = "选考题";
            break;
        case 13:
            typeNmae = "综合题";
            break;
        case 14:
            typeNmae = "选择填空";
            break;
        case 15:
            typeNmae = "材料填空题";
            break;
        case 16:
            typeNmae = "短文改错";
            break;
        case 17:
            typeNmae = "材料题";
            break;
        case 18:
            typeNmae = "材料填空";
            break;
        case 19:
            typeNmae = "听写题";
            break;
        case 20:
            typeNmae = "解答题";
            break;
        case 21:
            typeNmae = "句型转换题";
            break;
        case 22:
            typeNmae = "完形填空";
            break;
        case 23:
            typeNmae = "语法填空";
            break;
        case 24:
            typeNmae = "单词拼写";
            break;
        case 25:
            typeNmae = "词形转换";
            break;
        case 26:
            typeNmae = "默写";
            break;
        case 27:
            typeNmae = "还原句子";
            break;
        case 28:
            typeNmae = "补全对话";
            break;
        case 29:
            typeNmae = "填空题";
            break;
        case 30:
            typeNmae = "解答题";
            break;
        case 31:
            typeNmae = "判断改错";
            break;
        case 32:
            typeNmae = "名著导读";
            break;
        case 33:
            typeNmae = "听写题";
            break;
        case 34:
            typeNmae = "作文题";
            break;
        case 35:
            typeNmae = "实验题";
            break;
        case 36:
            typeNmae = "语言表达";
            break;
        case 37:
            typeNmae = "翻译";
            break;
        case 38:
            typeNmae = "推断题";
            break;
        case 39:
            typeNmae = "辨析题";
            break;
        case 40:
            typeNmae = "探究题";
            break;
        case 41:
            typeNmae = "分析说明题";
            break;
        case 42:
            typeNmae = "连线题";
            break;
        case 43:
            typeNmae = "口语题";
            break;
        case 44:
            typeNmae = "计算题";
            break;
        case 45:
            typeNmae = "口算题";
            break;
        case 46:
            typeNmae = "选词填空";
            break;
        case 47:
            typeNmae = "复合题";
            break;
        case 48:
            typeNmae = "诗歌鉴赏";
            break;
        case 49:
            typeNmae = "文言文阅读";
            break;
        case 50:
            typeNmae = "现代文阅读";
            break;
        case 51:
            typeNmae = "阅读题";
            break;
        case 52:
            typeNmae = "综合性学习";
            break;
        case 53:
            typeNmae = "阅读理解";
            break;
        case 54:
            typeNmae = "任务阅读";
            break;
        case 55:
            typeNmae = "综合题";
            break;
        default:
            typeNmae = "全部题型";
            break;
    }
    return typeNmae;
}
